import { defineStore } from 'pinia'
/**
 *
 */
export const useKeepAliveStore = defineStore('keepAliveStore', {
    state: (): KeepAliveNamesState => ({
        keepAliveNames: [],
        cachedViews: [],
    }),
    actions: {
        async setCacheKeepAlive(data: Array<string>) {
            this.keepAliveNames = data
        },
        async addCachedView(view: any) {
            if (view.meta.isKeepAlive) this.cachedViews?.push(view.name)
        },
        async delCachedView(view: any) {
            const index = this.cachedViews.indexOf(view.name)
            if (index > -1) {
                this.cachedViews.splice(index, 1)
            }
        },
        async delOthersCachedViews(view: any) {
            if (view.meta.isKeepAlive) this.cachedViews = [view.name]
            else this.cachedViews = []
        },
        async delAllCachedViews() {
            this.cachedViews = []
        },
    },
})
